package com.acwing.partition2;

import java.io.*;
import java.util.PriorityQueue;

/**
 * @author `RKC`
 * @date 2021/12/2 21:33
 */
public class AC148合并果子 {

    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
        String[] s = reader.readLine().split(" ");
        int n = Integer.parseInt(s[0]);
        s = reader.readLine().split(" ");
        int[] nums = new int[n];
        for (int i = 0; i < n; i++) {
            nums[i] = Integer.parseInt(s[i]);
        }
        writer.write(greedy(nums) + "\n");
        writer.flush();
    }

    private static int greedy(int[] nums) {
        int answer = 0;
        PriorityQueue<Integer> queue = new PriorityQueue<>();
        for (int num : nums) queue.add(num);
        while (!queue.isEmpty()) {
            int x = queue.poll();
            if (queue.isEmpty()) break;
            int y = queue.poll();
            answer += x + y;
            queue.add(x + y);
        }
        return answer;
    }
}
